/*
 * CCameraPreprocessor.h
 *
 *  Created on: 04.09.2011
 *      Author: Blinov Kirill
 *      Copyright: LGPL
 */

#ifndef CCAMERAPREPROCESSOR_H_
#define CCAMERAPREPROCESSOR_H_

#include "../BasicTypes.h"
#include "CRenderer.h"
#include "../TThreadSafeQueue.h"

/*
 *
 */
class CCameraPreprocessor
{
private:
	volatile bool ProcessingLoopControl;
	TThreadSafeQueue<FULL_FRAME_DATA*> *frameQueue;
	TThreadSafeQueue<INPUT_MESSAGE*>   *inputQueue; //Queue for messages from keyboard and mouse

	glm::vec3 cameraSphereCenter;
	float     cameraSphereRadius;
	glm::vec4 frustum[PARALLELEPIPED_PLANE_COUNT];
	glm::mat4 projectionMatrix;

	USHORT      previousMouseButtonState[MOUSE_KEY_COUNT];

	static bool CompareGameObjectsByDistance( GAME_OBJECT* First, GAME_OBJECT* Second )
	{
		return ( First->distanceToCamera < Second->distanceToCamera );
	}
	USHORT GetMouseButtonFromSDL( const USHORT SdlButton );
protected:
	void  ProcessingThread( CRenderer *Renderer );
	void  ProcessInput();
	void  CalculateClipPlanes( const glm::mat4 &CameraMatrix );
	bool  CutObjectByViewSphere( const glm::vec3 &SphereCenter, const float Radius );
	float CutObjectSphereByViewFrustum( const glm::vec3 &SphereCenter, const float Radius );
	bool  CutObjectBoxByViewFrustum( const glm::vec3 &FirstCorner, const glm::vec3 &SecondCorner );
public:
	CCameraPreprocessor( );
	virtual ~CCameraPreprocessor();

	bool Start( TThreadSafeQueue<FULL_FRAME_DATA*> *FrameQueue, TThreadSafeQueue<INPUT_MESSAGE*> *InputMessageQueue,
			    CRenderer *Renderer  );
	void Stop();
};

#endif /* CCAMERAPREPROCESSOR_H_ */
